package com.cxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_auctionMsg;
import com.common.entity.A_goods;
import com.common.entity.A_goodsState;
import com.common.entity.A_goodsType;
import com.common.entity.A_markup;
import com.common.entity.A_record;
import com.common.entity.A_timeRules;
import com.common.utils.StringUtils;
import com.cxh.dao.A_auctionMsgDao;
import com.cxh.dao.A_goodsDao;
import com.cxh.dao.A_goodsStateDao;
import com.cxh.dao.A_goodsTypeDao;
import com.cxh.dao.A_recordDao;
import com.cxh.dao.impl.A_auctionMsgDaoImpl;
import com.cxh.dao.impl.A_goodsDaoImpl;
import com.cxh.dao.impl.A_goodsStateDaoImpl;
import com.cxh.dao.impl.A_goodsTypeDaoImpl;
import com.cxh.dao.impl.A_recordDaoImpl;
import com.lyk.dao.A_markupDao;
import com.lyk.dao.A_timeRulesDao;
import com.lyk.dao.impl.A_markupDaoImpl;
import com.lyk.dao.impl.A_timeRulesDaoImpl;

public class A_auctionMsgServlet extends HttpServlet {
	A_goodsTypeDao gtd = null;
	A_auctionMsgDao amd = null;
	A_goodsDao gd = null;
	A_markupDao md = null;
	A_goodsStateDao gsd = null;
	A_timeRulesDao trd = null;
	A_recordDao rd = null;
	StringUtils su = null;
	@Override
	public void init() throws ServletException {
		gtd = new A_goodsTypeDaoImpl();
		amd = new A_auctionMsgDaoImpl();
		gd = new A_goodsDaoImpl();
		md = new A_markupDaoImpl();
		gsd = new A_goodsStateDaoImpl();
		trd = new A_timeRulesDaoImpl();
		rd = new A_recordDaoImpl();
		su = new StringUtils();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String opr = req.getParameter("opr");
		if("byid".equals(opr)||"bydate".equals(opr)||"id&date".equals(opr))
			findByCDT(req, resp, opr);
		else if("down".equals(opr)||"up".equals(opr))
			upanddown(req, resp, opr);
		else
			auctionMsgAll(req, resp);
	}
	private void upanddown(HttpServletRequest req, HttpServletResponse resp, String opr)
			throws IOException
	{
		int rows = 0;
		int auctionMsg_id = 0;
		String errmsg = "";
		A_auctionMsg a = new A_auctionMsg();
		Map<String, Object> mp = new HashMap<String, Object>();
		try{	
			auctionMsg_id = su.str2Integer(req.getParameter("auctionMsg_id"), null);
			a = amd.findById(auctionMsg_id);
			mp.put("auctionMsg_id", auctionMsg_id);
			mp.put("goodsState_id", "down".equals(opr)?0:1);
		}catch(Exception e){
			errmsg = e.getMessage();
		}
		rows = amd.updateA_auctionMsg(mp);
		PrintWriter out = resp.getWriter();
		if(rows>0)
			out.println("<script type='text/javascript'>alert('修改完成');location.href='auctionMsgServlet';</script>");
		else
			out.println("<script type='text/javascript'>alert('修改失败:"+errmsg+"');location.href='auctionMsgServlet';</script>");
	}
	private void findByCDT(HttpServletRequest req, HttpServletResponse resp, String opr)
			throws ServletException, IOException
	{
		String errmsg;
		int auctionMsg_id = 0;
		A_auctionMsg a = null;
		Map<String, Object> mp = new HashMap<String, Object>();
		List<A_auctionMsg> lam = null;
		java.util.Date start_date = new java.util.Date(0);
		try{	
			if("byid".equals(opr)){
				auctionMsg_id =  su.str2Integer(req.getParameter("auctionMsg_id"), null);
				mp.put("auctionMsg_id",auctionMsg_id);
			}else if("bydate".equals(opr)){
				mp.put("start_date",req.getParameter("start_date"));
			}else if("id&date".equals(opr)){
				auctionMsg_id =  su.str2Integer(req.getParameter("auctionMsg_id"), null);
				mp.put("auctionMsg_id",auctionMsg_id);
				mp.put("start_date",req.getParameter("start_date"));
			}
		}catch(Exception e){
			errmsg = e.getMessage();
		}
		lam = amd.findByCondition(mp);
		if(lam!=null&&lam.size()>0){	
			List<A_goodsType> lgt = gtd.findByCondition(null, true);
			req.setAttribute("lgt", lgt);
			List<A_goods> lg = gd.findByCondition(null);
			req.setAttribute("lg", lg);	
			List<A_markup> lm = md.findByCondition(null);
			req.setAttribute("lm", lm);
			List<A_timeRules> ltr = trd.findByCondition(null);
			req.setAttribute("ltr", ltr);
			List<A_goodsState> lgs = gsd.findByCondition(null);
			req.setAttribute("lgs", lgs);
			List<A_record> lr = rd.findByCondition(null);
			req.setAttribute("lr", lr);
		}
		req.setAttribute("lam", lam);
		req.getRequestDispatcher("AuctionManage/auctionHouse.jsp").forward(req, resp);
	}
	private void auctionMsgAll(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<A_goodsType> lgt = gtd.findByCondition(null, true);
		req.setAttribute("lgt", lgt);
		List<A_auctionMsg> lam = amd.findByCondition(null);
		req.setAttribute("lam", lam);
		List<A_goods> lg = gd.findByCondition(null);
		req.setAttribute("lg", lg);	
		List<A_markup> lm = md.findByCondition(null);
		req.setAttribute("lm", lm);
		List<A_timeRules> ltr = trd.findByCondition(null);
		req.setAttribute("ltr", ltr);
		List<A_goodsState> lgs = gsd.findByCondition(null);
		req.setAttribute("lgs", lgs);
		List<A_record> lr = rd.findByCondition(null);
		req.setAttribute("lr", lr);
		req.getRequestDispatcher("AuctionManage/auctionHouse.jsp").forward(req, resp);
	}
}